package top.tangyh.lamp.common.utils;

import cn.hutool.core.util.StrUtil;
import top.tangyh.basic.exception.BizException;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

/**
 * @Author:
 * @Date:
 */
public class IcuDateUtils {

    private static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static ZoneId myZone = TimeZone.getTimeZone("UTC").toZoneId();
    private static ZoneId currentZone = OffsetDateTime.now().getOffset();
    /**
     * 格式化 LocalDateTime 为UTC时区的字符串
     *
     * @param localTime
     * @return utc 时间
     * @Date 2023-08-05
     **/
    public static String getGivenZoneTimeStr(LocalDateTime localTime) {
//        System.out.println("转换前的时间：" + localTime.format(df));
        LocalDateTime newTime = localTime.atZone(currentZone).withZoneSameInstant(myZone).toLocalDateTime();
        return newTime.format(df);
    }

    /**
     * 根据天的时间戳和小时、分钟生成具体的时间戳，比如生成开始的和结束的时间戳
     *
     * @param dayTimestamp 精确到天的时间戳
     * @param hour
     * @param minute
     * @return
     */
    public static Long getTimestampByHourMinute(Long dayTimestamp, int day, int hour, int minute) {
        long dayMillisecond = getDayMillisecond(day);
        long hourMillisecond = getHourMillisecond(hour);
        long minuteMillisecond = getMinuteMillisecond(minute);
        return dayTimestamp + dayMillisecond + hourMillisecond + minuteMillisecond;
    }

    public static Long getTimestampByHourMinuteSeconds(Long dayTimestamp, int day, int hour, int minute, int seconds) {
        long dayMillisecond = getDayMillisecond(day);
        long hourMillisecond = getHourMillisecond(hour);
        long minuteMillisecond = getMinuteMillisecond(minute);
        long secondsMillisecond = getSecondsMillisecond(seconds);
        return dayTimestamp + dayMillisecond + hourMillisecond + minuteMillisecond + secondsMillisecond;
    }

    /**
     * 当前时间戳和小时计算
     * @param dayTimestamp
     * @param hour
     * @return
     */
    public static Long getTimestampByHour(Long dayTimestamp, int hour) {
        long hourMillisecond = getHourMillisecond(hour);
        return dayTimestamp + hourMillisecond;
    }

    public static Long getTimestampBeforeHour(Long dayTimestamp, int hour) {
        long hourMillisecond = getHourMillisecond(hour);
        return dayTimestamp - hourMillisecond;
    }

    /**
     * 获取小时
     *
     * @param time 格式 6:00
     * @return
     */
    public static int getHour(String time) {
        String[] split = time.split(":");
        return Integer.parseInt(split[0]);
    }

    /**
     * 获取分钟毫秒
     *
     * @param time 格式 6:00
     * @return
     */
    public static int getMinute(String time) {
        String[] split = time.split(":");
        return Integer.parseInt(split[1]);
    }

    /**
     * 获取天毫秒数
     *
     * @param day 天数
     * @return
     */
    public static long getDayMillisecond(int day) {
        return (long) day * 24 * 60 * 60 * 1000;
    }

    /**
     * 获取小时毫秒数
     *
     * @param hour 小时数
     * @return
     */
    public static long getHourMillisecond(int hour) {
        return (long) hour * 60 * 60 * 1000;
    }

    public static long getHourMillisecond(double hour) {
        return (long) (hour * 60 * 60 * 1000);
    }

    public static double getHoursByIntervalTimes(long timestamp) {
        double hours = (double) timestamp / (60 * 60 * 1000);
//        BigDecimal d = NumberUtil.round(hours, 5);
        return hours;
    }

    /**
     * 获取分钟的毫秒数
     *
     * @param minute 分钟数
     * @return
     */
    public static long getMinuteMillisecond(int minute) {
        return (long) minute * 60 * 1000;
    }

    public static long getSecondsMillisecond(int seconds) {
        return (long) seconds * 1000;
    }

    /**
     * 根据总分钟数和间隔分钟获取间隔次数
     *
     * @param totalMinutes
     * @param interval
     * @return
     */
    public static int getIntervalCounts(int totalMinutes, int interval) {
        return totalMinutes / interval;
    }

    /**
     * 根据开始和结束时间范围 获取之间的总分钟
     *
     * @param startHour   开始的小时
     * @param startMinute 开始的分钟
     * @param endHour     结束的小时
     * @param endMinute   结束的分钟
     * @return
     */
    public static int getTotalMinutesByHourMinute(int startHour, int startMinute, int endHour, int endMinute) {
        int total;
        //例如 6:00-12:00情况下 或 00:00 00:30
        if (startHour <= endHour) {
            if (startMinute > endMinute) {
                total = (endHour - startHour) * 60 - (startMinute - endMinute);
            } else {
                total = (endHour - startHour) * 60 + (endMinute - startMinute);
            }
        } else {
            //例如 18:00-2:00情况下
            if (startMinute > endMinute) {
                total = (24 - startHour) * 60 + endHour * 60 - (startMinute - endMinute);
            } else {
                total = (24 - startHour) * 60 + endHour * 60 + (endMinute - startMinute);
            }
        }
        return total;
    }

    /**
     * 根据开始和结束时间戳计算总分钟数
     *
     * @param startTimestamp
     * @param endTimestamp
     * @return
     */
    public static int getTotalMinutesByStartEndTimestamp(Long startTimestamp, Long endTimestamp) {
        return (int) (endTimestamp - startTimestamp) / (60 * 1000);
    }

    /**
     * 检查设置的间隔不能超过起始时间最大的间隔分钟数
     *
     * @param startHour   开始的小时
     * @param startMinute 开始的分钟
     * @param endHour     结束的小时
     * @param endMinute   结束的分钟
     * @param interval    间隔的分钟数
     */
    public static void checkInterval(int startHour, int startMinute, int endHour, int endMinute, int interval) {
        if (interval < 0) {
            throw new BizException("间隔时间必修大于0");
        }
        int totalMinutes = getTotalMinutesByHourMinute(startHour, startMinute, endHour, endMinute);
        if (interval >= totalMinutes) {
            throw new BizException("间隔时间不能超过起始时间的最大间隔");
        }
    }

    /**
     * 根据开始和结束时间戳范围、间隔次数、间隔，获取具体时间点集合
     *
     * @param startTimestamp 开始时间戳
     * @param endTimestamp   结束时间戳
     * @param counts         间隔次数
     * @param interval       间隔分钟数
     * @return
     */
    public static Set<Long> getAllTimePointsByTimeRange(long startTimestamp, long endTimestamp, int counts, int interval,String moduleId) {
        Set<Long> timePointsSet = new HashSet<>();
        for (int i = 1; i <= counts; i++) {
            //依次添加到时间集合中
            long timePoint = startTimestamp + getMinuteMillisecond(i * interval);
            timePointsSet.add(timePoint);
        }
        if(StrUtil.isNotEmpty(moduleId) && moduleId.equals("crl")){
            timePointsSet.add(endTimestamp);
            timePointsSet.add(endTimestamp+getMinuteMillisecond(interval));
        }
        else{
            timePointsSet.add(startTimestamp);
            timePointsSet.add(endTimestamp);
        }
        return timePointsSet;
    }

}
